package com.ruoyi.business.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.business.Customer;
import com.ruoyi.business.enums.DealEnum;
import com.ruoyi.business.mapper.CustomerMapper;
import com.ruoyi.business.qo.CustomerQO;
import com.ruoyi.business.vo.AdminVO;
import com.ruoyi.business.vo.CustomerVO;
import com.ruoyi.common.common.CommonEnum;
import com.ruoyi.common.common.GlobalValue;
import com.ruoyi.common.common.ResultBody;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * @author ：jiangguanjun
 * @description：管理员相关
 * @date ：2020/7/16 21:13
 */
@Slf4j
@Component
public class CustomerService  extends ServiceImpl<CustomerMapper, Customer> {

    @Autowired
    private CustomerMapper customerMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private AdminService adminService ;


    public ResultBody add(Customer customer) {
        int res = customerMapper.insertSelective(customer) ;
        if(res>0){
            String repeatKey = GlobalValue.getCustomerRedisKey(customer.getCuIp()) ;
            redisTemplate.opsForValue().set(repeatKey,customer.getCuIp(),1L, TimeUnit.MINUTES);

            return ResultBody.success();
        }

        return ResultBody.error(CommonEnum.CUSTOMER_ADD_ERROR) ;
    }


    public ResultBody queryPage(CustomerQO customerQO) {

        Page<CustomerVO> page = new Page<>(customerQO.getPageNum(),customerQO.getPageSize()) ;
        IPage<CustomerVO> iPage = customerMapper.queryPage(page,customerQO) ;
        // 组装管理员信息
        List<CustomerVO> customerList = iPage.getRecords() ;
        if(CollectionUtils.isEmpty(customerList)){
            return ResultBody.success(iPage) ;
        }

        customerList.stream().forEach(customerVO -> {
            log.info("customerVO:::::{}",customerVO.getAdminId());
        });

        Set<Long> adminIds = customerList.stream().map(CustomerVO::getAdminId).collect(Collectors.toSet()) ;
        log.info("adminIds:::{}",adminIds);
        if(!CollectionUtils.isEmpty(adminIds) && adminIds.size()>0){
            List<AdminVO> adminVOS = new ArrayList<>();
            try {
                adminVOS = adminService.getBatchByIds(adminIds) ;
            }catch (Exception e){
                log.error("获取管理员信息失败",e);
            }

            if(!CollectionUtils.isEmpty(adminVOS) && adminVOS.size()>0){
                List<AdminVO> finalAdminVOS = adminVOS;
                customerList.forEach(customer -> {
                    finalAdminVOS.forEach(adminVO -> {
                        if(null != customer.getAdminId() && customer.getAdminId().equals(adminVO.getId())){
                            customer.setAdminName(adminVO.getName());
                        }
                    });
                });
            }
        }

        iPage.setRecords(customerList) ;


        return ResultBody.success(iPage) ;
    }

    public ResultBody deal(Integer id,Integer op,Long adminId) {
        Customer customer = new Customer() ;
        customer.setDeal(DealEnum.DEAL_FINAL.getCode());
        customer.setId(id);
        customer.setEffective(op);
        customer.setAdminId(adminId);
        int res = customerMapper.updateByPrimaryKeySelective(customer) ;
        if(res>0){
            return ResultBody.success() ;
        }
        return ResultBody.success(CommonEnum.CUSTOMER_DEAL_ERROR) ;
    }
    public Customer getById(Integer id){
        return customerMapper.selectByPrimaryKey(id) ;
    }
}
